//
//  HHTextMessageCell.m
//  HHChatKit
//
//  Created by Henry on 2020/8/13.
//  Copyright © 2020 Henry. All rights reserved.
//

#import "HHTextMessageCell.h"
#import "UILabel+YBAttributeTextTapAction.h"

@interface HHTextMessageCell () <YBAttributeTapActionDelegate>

@end

@implementation HHTextMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];
    if (self) {
        _content = [[UILabel alloc] init];
        _content.numberOfLines = 0;
        [self.bubbleView addSubview:_content];
        
        self.bubbleView.userInteractionEnabled = YES;
        
    }
    return self;
}

- (void)fillWithData:(HHTextMessageCellData *)data {
    //set data
    [super fillWithData:data];
    self.textData = data;
    self.content.textColor = data.textColor;
    self.content.attributedText = data.attributedString;
    
    NSArray *arr = [data getAttributeRange:data.content];
    [_content yb_addAttributeTapActionWithRanges:arr delegate:self];

}

#pragma mark - YBAttributeTapActionDelegate

- (void)yb_tapAttributeInLabel:(UILabel *)label string:(NSString *)string range:(NSRange)range index:(NSInteger)index {
//    DLog(@"yb_tapAttributeInLabel: %@", string);
    
    // 手机号
//    if ([string jk_isMobileNumber]) {
//        NSString *message = [NSString stringWithFormat:@"%@可能是一个电话号码,你可以", string];
//        [AlertCustomTool sheetWithMessage:message buttonTitles:@[@"呼叫", @"复制号码"] actionsBlock:^(NSInteger buttonIndex, NSString * _Nonnull buttonTitle) {
//            if (buttonIndex == 1) {
//                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//                pasteboard.string = string;
//            } else if (buttonIndex == 0) {
//                // TODO iPad
//                NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@", string];
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
//            }
//        }];
//    } else if ([string jk_isValidUrl]) {
//        if (self.delegate && [self.delegate respondsToSelector:@selector(onSelectLinkUrl:string:)]) {
//            [self.delegate onSelectLinkUrl:self string:string];
//        }
//    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.content.frame = (CGRect){.origin = self.textData.textOrigin, .size = self.textData.textSize};
}

@end
